package com.lanhai.longheng.impl;
  
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanhai.longheng.beans.AgvBean;
import com.lanhai.longheng.beans.DotBean;
import com.lanhai.longheng.dao.PowerDao;
import com.lanhai.longheng.dao.RoadDeviceDao;
import com.lanhai.longheng.interfaces.IAgv;
import com.lanhai.longheng.interfaces.IAgvEvent; 
import com.lanhai.longheng.interfaces.IDot;
import com.lanhai.longheng.interfaces.IPower;
import com.lanhai.longheng.mapper.DotMapper;
import com.lanhai.longheng.mapper.PowerMapper;
import com.lanhai.longheng.mapper.RoadMapper;
 

@Service
public class PowerImpl implements IPower{
	@Autowired
	PowerMapper powerMapper;

	@Autowired
	IDot iDot;
	
	@Autowired
	IAgv iAgv;
	
	@Autowired
	IAgvEvent iAgvEvent;
	 
	 
	@Autowired
	RoadMapper roadMapper;
	
	@Autowired
	DotMapper dotMapper;
	 
	
	@Override
	public synchronized void unlockForAgv(int agvid) { 
		powerMapper.unlockForAgv(agvid);
		
	}

	 
	/**
	 * 小车回到工作岗位
	 */
	@Override
	public synchronized boolean agvBackToWork(int min) { 
		long t=System.currentTimeMillis()-min*60000;
		PowerDao power=powerMapper.findPowerfulAgv(t);
		if(power==null) {
			return false;
		}
	 
		AgvBean agv=iAgv.getBean(power.getAgvid());
		if(agv==null) { 
			return false;
		}
		DotBean dotb=iDot.getBean(power.getDotno());
		if(dotb==null) { 
			return false;
		} 
		 
		powerMapper.unlockPower(dotb.getDotNO());
		
		//回去的路
		agv.setNextRoad(dotb.getRouteRoad());
		agv.setFinishTrans(true); 
		iAgv.saveCache(agv);
		iAgvEvent.stopTrans(agv); 
		return true;
	}
 
 
	@Override
	public  void agvArrivedPower(int agvid,int dotno) {
		//解锁路段
		Integer decide=powerMapper.getDecideDot(dotno);
		if(decide!=null&&decide>0) { 
			//再锁一次充电桩
			powerMapper.lockPower(dotno, agvid, System.currentTimeMillis());
		}
		 
	}

	/**
	 * 1=成功  0=占用中  -1=没有可用（不存在或者禁用）
	 */
	@Override
	public int agvToPower(int currentDot, AgvBean agv) {
		 
		
		Integer transdot=powerMapper.getEmptyDot(currentDot); //拿当前点位对应的可用的一个充电桩
		if(transdot==null||transdot<=0) { 
			return -1;
		} 
		 
		//找路线 
		RoadDeviceDao roadDao=roadMapper.getTransRoad(currentDot, transdot); //拿路线
		if (roadDao == null ||  roadDao.getRoad() <=  0) {
			  
			return -1;
		}  
		agv.setNextProcess("");
		agv.setNextRoad(roadDao.getRoad());
		agv.setNextNode(  "去充电" );
		iAgv.saveCache(agv);
		
		powerMapper.lockPower(transdot, agv.getAgvId(), System.currentTimeMillis());
		iAgvEvent.toRoad(agv, roadDao.getRoad());
		return 1;
	}


	@Override
	public void unlockPowerByID(long id) {
		PowerDao power=powerMapper.getDaoByID(id);
		if(power==null) {
			return  ;
		}
		//解锁
		powerMapper.unlockPowerByID(id);
		
		if (power.getAgvid() > 0) {
			// 充电桩有小车，让小车回去 
		 
			// 小车返回 
			AgvBean agv = iAgv.getBean(power.getAgvid());
			if (agv == null) {
				return;
			}
			DotBean dotb = iDot.getBean(power.getDotno());
			if (dotb == null) {
				return;
			}
			//回去的路
			agv.setNextRoad(dotb.getRouteRoad());
			agv.setFinishTrans(true); 
			agv.setNextNode( "返回工作岗位" );
		 
			
			iAgv.saveCache(agv);
			iAgvEvent.stopTrans(agv);
		}
	}
 

 
}
